package com.newlink.building.record

import android.text.TextUtils
import android.view.View
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.newlink.building.common_base.base.NL_BaseActivity
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.common_base.utils.TimestampUtils
import com.newlink.building.common_base.widget.CustomDialog
import com.newlink.building.manager.Module_Phone_VoipController
import com.newlink.building.record.databinding.ActivityCaptureVideoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * @author wangyang
 * @date 2023/4/19
 * @description记录——截图记录——详情页面
 */
class Module_Record_CaptureVideoDetailActivityNL : NL_BaseActivity() {

//    companion object {
//        /**
//         * 对外开放统一入口
//         *
//         * @param context
//         * @param data     String
//         */
//        fun startSelf(context: Context?, data: String) {
//            val intent = Intent(context, CaptureVideoDetailActivity::class.java)
//            intent.putExtra("data", data)
//            context?.startActivity(intent)
//        }
//    }

    private lateinit var mBinding: ActivityCaptureVideoBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityCaptureVideoBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {
    }

    override fun initView() {
        val data = intent?.getStringExtra("data")

        if (!TextUtils.isEmpty(data)) {
            if (data?.contains("/") == true) {
                val split1 = data.split("/")
                val fileName = split1[split1.size - 1]
                if (fileName.contains("@@@")) {
                    val split2 = fileName.split("@@@")

                    mBinding.titleBar.tvTitleBarCenter.text =
                        split2[0]
                    mBinding.titleBar.tvTitleBarCenter.ellipsize = TextUtils.TruncateAt.MARQUEE
                    mBinding.titleBar.tvTitleBarCenter.marqueeRepeatLimit = -1
                    mBinding.titleBar.tvTitleBarCenter.isSelected = true

                    val timeStr = split2[1]
                    if (timeStr.contains(".")) {
                        val splitDate = timeStr.split(".")[0]
                        mBinding.titleBar.tvSubtitleBarCenter.text =
                            TimestampUtils.callLogTimeFormat(splitDate.toLong(), false)
                    }

                    mBinding.titleBar.ivTitleBarRight.apply {
                        visibility = View.VISIBLE
                        setBackgroundResource(R.drawable.ic_delete)
                        setOnClickListener {
                            CustomDialog(this@Module_Record_CaptureVideoDetailActivityNL)
                                .setMessage(getString(R.string.dialog_delete_screenshot))
                                .setPositiveButton(object : CustomDialog.OnClickListener {
                                    override fun onClick(dialog: CustomDialog) {
                                        deleteScreenshot(data)
                                        dialog.dismiss()
                                    }
                                }).setNegativeButton(object : CustomDialog.OnClickListener {
                                    override fun onClick(dialog: CustomDialog) {
                                        dialog.dismiss()
                                    }
                                }).show()
                        }
                    }
                }
            }
        } else {
            findViewById<TextView>(R.id.tv_title_bar_center).text =
                resources.getText(R.string.capture_video)
        }
        Glide
            .with(this)
            .load(data)
            .placeholder(R.color.black)
            .into(mBinding.ivPicture)
    }

    private fun deleteScreenshot(data: String) {
        lifecycleScope.launch(Dispatchers.IO) {
            val res = Module_Phone_VoipController.get().removeScreenshot(data)
            NLog.e("[Capture] >>  delete screenshot result is $res")
            withContext(Dispatchers.Main) {
                setResult(RESULT_OK)
                this@Module_Record_CaptureVideoDetailActivityNL.finish()
            }
        }
    }

}